﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json; 
using T.FAS.Runtime.Base.Entity;
using T.ServiceComponent.DAC.Service;
using System.Reflection;
using System.Data;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Base.Dao
{
    public class IconLibraryELBDao : DaoService<IconLibrary>
    {
        public override string TableName => "FAS_IconLibrary";
        public override string IDColumnName => "ID";
        public override List<string> ExceptColumnName => new List<string>
        { 
        };        
         
        public IconLibraryELBDao() : base()
        {
            DBName = "TFAS";
        }

        public override IconLibrary ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);

            myCol.Remove("IconType");

            var data = base.ConvertToObject(myCol, row);

            if (row["IconType"] != null)
                data.IconType = (IconType)Enum.Parse(typeof(IconType),
                                                Convert.ToString(row["IconType"]));

            return data;
        }

        public List<IconLibrary> GetIconLibrarys(Dictionary<string, string> filter)
        {
            var result = new List<IconLibrary>();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = "1 = 1";
            if (filter.ContainsKey("TenantID") && !string.IsNullOrEmpty(filter["TenantID"]))
            {
                sqlCondition = string.Format("{0} and TenantID=@TenantID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@TenantID", TDbDataType.VarChar, filter["TenantID"]));
            }
            if (filter.ContainsKey("icontype") && !string.IsNullOrEmpty(filter["icontype"]))
            {
                sqlCondition = string.Format("{0} and IconType=@IconType", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@IconType", TDbDataType.VarChar, filter["icontype"]));
            }
            if (filter.ContainsKey("condition") && !string.IsNullOrEmpty(filter["condition"]))
            {
                sqlCondition = string.Format("{0} and (Code like @Condition Or Name like @Condition)", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@Condition", TDbDataType.VarChar, "%"+filter["condition"] +"%"));
            }
            var ds = this.DB.ExecuteDataSet(string.Format("SELECT ID, Code, Name, IconType, FontClass, FontName, IconThumbnail, IconUrl, IconExtention, IconSize, SortCode, Category, CategorySortCode, TenantID, Creator, CreateTime, LastModifier, LastModifyTime, CommonUsed FROM {0} WHERE {1} Order by CategorySortCode,SortCode", TableName,sqlCondition),paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    result.Add(ConvertToObject(this.Columns, row));
                }
            }
            return result;
        }        

        public int CheckIconLibraryUnique(Dictionary<string, string> filter)
        {
            return (int)(long)this.DB.ExecuteScalar($@"select count(1) from FAS_IconLibrary
            where Code ='{filter["Code"]}' AND TenantID='{filter["TenantID"]}'");
        }
    }
}
